package cn.edu.fzu.delivery.controller.networkadmin;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.common.RetCode;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.SexEnum;
import cn.edu.fzu.delivery.domain.enums.UserStatusEnum;
import cn.edu.fzu.delivery.domain.enums.UserTypeEnum;
import cn.edu.fzu.delivery.service.UserService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 网点管理员-快递员管理
 *
 * @author fangxin
 * @date 2022/11/15
 */
@EnableOpenApi
@Api(tags = "网点管理员-快递员管理接口")
@RestController
@RequestMapping("/api/auth/networkAdmin/deliveryMan")
public class DeliveryManController {

    @Resource
    private UserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "网点管理员：获取快递员接口", httpMethod = "GET")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true), @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常"),
            @ApiImplicitParam(name = "phone", value = "手机号"), @ApiImplicitParam(name = "email", value = "邮箱") })
    public Result list(String name, UserStatusEnum status, String phone, String email, Integer pageNum,
                       Integer pageSize, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<SysUser> page = PageHelper.startPage(pageNum, pageSize)) {
            userService.listSysUser(currentUser.getNetwork().getId(), name, UserTypeEnum.EXPRESS, status, phone, email,
                    currentUser);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "更新快递员信息接口", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "快递员id", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱"), @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "birth", value = "出生年月日"), @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "address", value = "家庭住址"),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常"),
            @ApiImplicitParam(name = "sex", value = "-1-保密，0-男，1-女，2-其他") })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result update(Long id, String email, String name, SexEnum sex, Date birth, String phone, String address,
                         UserStatusEnum status, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        userService.updateSysUserInfo(id, email, name, sex, birth, phone, address, null, status, null,
                currentUser);
        return new Result(RetCode.SUCCESS, "修改成功");
    }
}
